package com.gmss.crawler.module.social.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gmss.crawler.module.person.domain.PersonInfo;
import com.gmss.crawler.module.social.domain.Social;
import com.gmss.crawler.module.social.mapper.SocialMapper;
import com.gmss.crawler.module.social.service.ISocialService;
import com.gmss.crawler.utils.CommonUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 社会任职表 服务实现类
 * </p>
 *
 * @author Auto-generator
 * @since 2021-10-19
 */
@Service
public class SocialServiceImpl extends ServiceImpl<SocialMapper, Social> implements ISocialService {

    @Override
    public void saveSocial(PersonInfo personInfo, List<Social> list) {
        for (int i = list.size() - 1; i >= 0; i--) {
            Social social = list.get(i);
            if (StringUtils.isBlank(social.getDuties())) {
                list.remove(i);
            } else {
                social.setPersonId(personInfo.getId());
            }
        }
        LambdaQueryWrapper<Social> honorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        honorLambdaQueryWrapper.eq(Social::getPersonId, personInfo.getId());
        int count = count(honorLambdaQueryWrapper);
        if (count > 0) {
            remove(honorLambdaQueryWrapper);
        }
        saveBatch(list);
    }

    @Override
    public void querySocialSave(PersonInfo personInfo, String data) {
        Social honor = new Social().builder().personId(personInfo.getId()).duties(data).build();
        LambdaQueryWrapper<Social> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Social::getPersonId, personInfo.getId());
//        wrapper.eq(Social::getDuties, data);
//        List<Social> resumeList = list(wrapper);
//        if (CollectionUtils.isEmpty(resumeList))
        if (count(wrapper) <= 0)
            save(honor);
    }

    /**
     * 查询personId在Social（社会任职）表中是否有数据，如果没有插入，有则不处理。
     *
     * @param personInfo
     * @param list
     */
    @Override
    public void saveSocialByPersonId(PersonInfo personInfo, List<Social> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            LambdaQueryWrapper<Social> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Social::getPersonId, personInfo.getId());
            int count = count(queryWrapper);
            if (count <= 0) {
                for (Social queryData : list) {
                    queryData.setPersonId(personInfo.getId());
                    save(queryData);
                }
            }
        }
    }
}
